package com.iflytek.dao.Impl;

import com.iflytek.bean.Emp;
import com.iflytek.dao.EmpDao;
import com.iflytek.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.List;



public class EmpDaoImpl implements EmpDao {


    @Override
    public List<Emp> getAllEmp()  {
        List<Emp> empList=null;
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());

        try {
            empList = queryRunner.query("select * from emp", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return empList;
    }

    @Override
    public void addEmp(Emp emp) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        Object [] object={
                emp.getEmpno(),
                emp.getEname(),
                emp.getJob(),
                emp.getSal(),
                new Date(System.currentTimeMillis()),
                emp.getComm(),
                emp.getDeptno()
        };
        try {
            int add = queryRunner.update("insert into emp value(?,?,?,?,?,?,?) ",object);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void upDate(int empno, Object object1, String updateChoose) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        Object [] object={object1,empno};
        try {
            switch (updateChoose){
                case "ename":
                    int update1 = queryRunner.update("update emp set ename=? where empno=?",object);
                    break;
                case "job":
                    int update2 = queryRunner.update("update emp set job=? where empno=?",object);
                    break;
                case "sal":
                    int update3 = queryRunner.update("update emp set sal=? where empno=?",object);
                    break;
                case "comm":
                    int update4 = queryRunner.update("update emp set comm=? where empno=?",object);
                    break;
                case "deptno":
                    int update5 = queryRunner.update("update emp set deptno=? where empno=?",object);
                    break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delEmpById(int empno) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        try {
            int delEmpById = queryRunner.update("delete from emp where empno = ?",empno);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Emp> selectLikeName(String ename) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> empList = null;
        String one = "%"+ename+"%";
        try {
            empList = queryRunner.query("select * from emp where ename like ?",new BeanListHandler<Emp>(Emp.class),one);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return empList;
    }

}
